#include <QApplication>
#include <QFontDatabase>
#include <QTranslator>
#include <QLocale>
#include <QDir>
#include "mainwindow.h"
#include "settingsmanager.h"
#include "translationsmanager.h"

int main(int argc, char *argv[])
{
    // 启用高DPI支持
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QApplication app(argc, argv);
    app.setApplicationName("Markdown Editor");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("MarkdownTools");
    app.setOrganizationDomain("markdowntools.example.com");

    // 加载设置
    SettingsManager settings;
    settings.load();

    // 设置主题
    if (settings.theme() == SettingsManager::DarkTheme) {
        // 加载深色主题样式表
        QFile styleFile(":/styles/dark.qss");
        if (styleFile.open(QFile::ReadOnly)) {
            QString style = QLatin1String(styleFile.readAll());
            app.setStyleSheet(style);
        }
    }

    // 加载翻译
    TranslationsManager translations;
    translations.loadLanguage(settings.language());

    // 加载自定义字体
    int fontId = QFontDatabase::addApplicationFont(":/fonts/SourceCodePro-Regular.ttf");
    if (fontId != -1) {
        QString fontFamily = QFontDatabase::applicationFontFamilies(fontId).at(0);
        QFont font(fontFamily);
        app.setFont(font);
    }

    // 创建并显示主窗口
    MainWindow mainWindow;
    mainWindow.resize(settings.windowSize());
    mainWindow.move(settings.windowPosition());
    mainWindow.show();

    // 运行应用程序事件循环
    int result = app.exec();

    // 保存窗口状态
    settings.setWindowSize(mainWindow.size());
    settings.setWindowPosition(mainWindow.pos());
    settings.save();

    return result;
}